home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 282_01 / quipadd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-11  |  2.1 KB  |  91 lines

  1. /*
  2. HEADER:        ;
  3. TITLE:         QUIP;
  4. VERSION:       2.0;
  5. DESCRIPTION:   "Part of a fortune cookie system
  6.  
  7.                This program appends new quips to the end of the quip file 'quip.dat',
  8.                and updates the index file 'quip.key'.";
  9. KEYWORDS:      quip, fortune, utility, startup;
  10. SYSTEM:        MS-DOS, UNIX;
  11. FILENAME:      quipadd.c;
  12. WARNINGS:      "";
  13. SEE-ALSO:      QUIP.C, QUIP.H, QUPUPDT.C QUIPADD.C, QUIP.DAT, QUIP.KEY;
  14. AUTHORS:       ?;
  15. MODIFIED BY:   David Bryant;
  16. COMPILERS:     Microsoft Quick C;
  17. */
  18.  
  19.  
  20. #include <stdio.h>
  21.  
  22. FILE  *seekfp,    /* the address file */
  23.       *quipfp;    /* the actual fortune file */
  24.  
  25. #include "quip.h"
  26.  
  27. char  buffer[MAX_SIZE];
  28.  
  29. openfiles()
  30. {
  31.    seekfp = fopen(seekname, "r+b");
  32.    if (seekfp == 0) {
  33.       puts("Cannot open quip address file.");
  34.       exit(0);
  35.    }
  36.    quipfp = fopen(quipname, "r+");       /* Open as TEXT file */
  37.    if (quipfp == 0) {
  38.       puts("Cannot open quip data file.");
  39.       exit(0);
  40.    }
  41. }
  42.  
  43. putaddr(addr)
  44. long  addr;
  45. {
  46.    if (fwrite(&addr, sizeof(long), 1, seekfp) < 1)
  47.       printf("\nWrite error on address file!\n");
  48. }
  49.  
  50. addquip()
  51. {
  52.    int   i = 0,
  53.          c;
  54.    if (fseek(seekfp, 0L, 2) / 2 > 32767L) {
  55.       puts("Sorry, the quip file has too many entries.");
  56.       return;
  57.    }
  58.    fseek(quipfp, 0L, 2);
  59.    printf("Add your quip and end with a %c.\n", SEPERATOR);
  60.    printf("Each quip can be a maximum of %d characters long.\n\n", MAX_SIZE);
  61.    while (((c = getchar()) != SEPERATOR) && i <= MAX_SIZE)
  62.       buffer[i++] = c;
  63.    fprintf(quipfp, "%c\n", SEPERATOR);
  64.    putaddr(ftell(quipfp));
  65.    if (fwrite(buffer, i, 1, quipfp) != 1) {
  66.       printf("Write of quip failed!\n");
  67.       exit(2);
  68.    }
  69. }
  70.  
  71. closefiles()
  72. {
  73.    fclose(seekfp);
  74.    fclose(quipfp);
  75. }
  76.  
  77. main()
  78. {
  79. #ifdef UNIX
  80.    if (!strcmp(getlogin(), "demo") || !strcmp(getlogin(), "intro"))
  81.       {
  82.       printf("Sorry, but demo cannot add quips.\n");
  83.       exit(0);
  84.       }
  85. #endif
  86.    openfiles();
  87.    addquip();
  88.    closefiles();
  89. }
  90.  
  91.